using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pegasus.Runtime.Serialization;

namespace Pegasus.UnitTests.Runtime.Serialization
{
	/// <summary>
	/// This class test the object serialization binder.
	/// </summary>
	[TestClass]
	public class ObjectBinderTest
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:ObjectBinderTest"/> class.
		/// </summary>
		public ObjectBinderTest()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Test the bind object.
		/// </summary>
		[TestMethod]
		public void BindingTest()
		{
			ObjectSerializationBinder binder = new ObjectSerializationBinder();

			// Try current version of .NET Type
			string assemblyName = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
			string typeName = "System.Int32";

			Type type = binder.BindToType( assemblyName, typeName );
			Assert.IsNotNull( type, "could not match current int type" );
			Assert.IsTrue( type == typeof( System.Int32 ), "Int32 type not matched" );

			// Try different verison of .NET type
			assemblyName = "mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
			typeName = "System.Int32";

			type = binder.BindToType( assemblyName, typeName );
			Assert.IsNotNull( type, "could not match previous int type" );
			Assert.IsTrue( type == typeof( System.Int32 ), "Int32 type not matched" );

			// Try a type from pegasus that should match
			assemblyName = "Pegasus.Library, Version=0.0.0.1, Culture=neutral, PublicKeyToken=a7ad7231644e419b";
			typeName = "Pegasus.DateTimeReoccurringDaily";

			type = binder.BindToType( assemblyName, typeName );
			Assert.IsNotNull( type, "could not match DateTimeReoccurringDaily type" );
			Assert.IsTrue( type == typeof( Pegasus.DateTimeReoccurringDaily ), "DateTimeReoccurringDaily type not matched" );
		}

		/// <summary>
		/// Test the binding object for unknown types.
		/// </summary>
		[TestMethod]
		public void UnknownBindingTest()
		{
			ObjectSerializationBinder binder = new ObjectSerializationBinder();
			binder.UnknownTypeReplacement = true;

			// Unknown type, but valid assembly
			string assemblyName = "Pegasus.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b";
			string typeName = "JunkType1";

			// Should match to Object
			Type type = binder.BindToType( assemblyName, typeName );
			Assert.IsNotNull( type, "could not match Object type" );
			Assert.IsTrue( type == typeof( System.Object ), "Object type not matched" );

			// Unknown type and invalid assembly
			assemblyName = "Does.Not.Exist, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b";
			typeName = "JunkType2";

			// Should match to Object
			type = binder.BindToType( assemblyName, typeName );
			Assert.IsNotNull( type, "could not match Object type" );
			Assert.IsTrue( type == typeof( System.Object ), "Object type not matched" );

		}
	}
}
